package com.ssm.pre.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.pre.util.PageCount;
import com.ssm.pre.util.ToolUtil;
import com.ssm.pre.dao.SystemDao;
import com.ssm.pre.service.SystemService;
import com.ssm.util.Constants;
import com.ssm.util.object.InputObject;
import com.ssm.util.object.OutputObject;

@Service
public class SystemServiceImpl implements SystemService {

	@Autowired
	private SystemDao systemDao;

	/**
	 * 查询一级菜单
	 */
	@Override
	public void queryMenuByOneList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		map.put("parentId", Constants.PERMISSION_PARENT_ID_ZERO);//parentId为0
		map.put("type", Constants.PERMISSION_MENU_FIRST);//一级菜单
		List<Map<String, Object>> beans = systemDao.queryMenuByOneList(map);
		int page = Integer.parseInt(map.get("offset").toString())/Integer.parseInt(map.get("limit").toString());
		page++;
		List<Map<String, Object>> params = PageCount.page(beans, page, Integer.parseInt(map.get("limit").toString()));
		outputObject.setBeans(params);
		outputObject.settotal(beans.size()); 
	}

	/**
	 * 查询一级菜单，用作select下拉框选择
	 */
	@Override
	public void queryMenuByOneSelect(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		map.put("type",Constants.PERMISSION_MENU_FIRST);//一级菜单
		List<Map<String, Object>> beans = systemDao.queryMenuByOneSelect(map);
		outputObject.setBeans(beans);
		
	}
	
	/**
	 * 查询二级菜单
	 */
	@Override
	public void queryMenuByTwoList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = systemDao.queryMenuByTwoList(map);
		outputObject.setBeans(beans);
	}

	/**
	 * 根据ID查询一条信息
	 */
	@Override
	public void queryMenuById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		Map<String, Object> bean = systemDao.queryMenuById(map);
		outputObject.setBean(bean);
		
	}

	
	/**
	 * 添加菜单
	 */
	@Override
	public void addMenu(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		map.put("createTime", ToolUtil.getTimeAndToString());
		map.put("createId", inputObject.getLogParams().get("id"));
		map.put("menuSort", Integer.parseInt((String)map.get("menuSort")));
		Map<String, Object> bean = systemDao.queryMenuByTypeAndSort(map);//根据类别和序号查询信息
		if(bean == null){
			if(map.get("menuType").toString().equals(Constants.PERMISSION_MENU_FIRST)){//一级菜单
				map.put("parentId", Constants.PERMISSION_PARENT_ID_ZERO);
				systemDao.addMenu(map);
			}else if(map.get("menuType").toString().equals(Constants.PERMISSION_MENU_SECOND)){//二级菜单
				systemDao.addMenu(map);
			}else{
				outputObject.setreturnMessage("非法的菜单级别.");
			}
		}else{
			outputObject.setreturnMessage("该菜单序号已存在，请修改后保存.");
		}
		
	}

	/**
	 *  根据ID删除信息
	 */
	@Override
	public void deleteMenuById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		if(map.get("menuBelong").toString().equals("0")){
			map.put("menuBelong", map.get("id"));
			systemDao.deleteMenuByParentId(map);//删除所属一级的二级菜单
		}
		systemDao.deleteMenuById(map);//根据ID删除信息
	}

	/**
	 * 根据ID修改一条菜单信息
	 */
	@Override
	public void updateMenuById(InputObject inputObject, OutputObject outputObject) throws Exception{
		Map<String, Object> map = inputObject.getParams();
		map.put("createTime", ToolUtil.getTimeAndToString());
		map.put("createId", inputObject.getLogParams().get("id"));
		map.put("menuSort", Integer.parseInt((String)map.get("menuSort")));
		
		systemDao.updateMenuById(map);
		
	}

}
